var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({
    /**
     * 页面的初始数据
     */
    data: {
        hasMore:true,
        pageSize:10,
        pageNo:1,
        status:'',
        tabs: [],
        activeTab: 0,
        orderList:[],
        dateStart:'',
        dateEnd:'',
        textStart:'开始日期',
        textEnd:'截止日期',
        btnStatus:false,
        supplierId:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const titles = ['全部订单', '待配送', '已配送', '售后']
        const tabs = titles.map(item => ({title: item}))
        this.setData({tabs})
        this.setData({
            supplierId:options.supplierId
        })
        this.getData(this.data.status)
    },
    onPullDownRefresh: function () {
        this.getData(this.data.status)
    },
    bindDateChange: function(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
          dateStart: e.detail.value
        })
    },
    bindDateChange1: function(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
          dateEnd: e.detail.value
        })
    },
    //搜索
    handleSearch(){
        if(this.data.dateStart == ''){
            wx.showToast({
                icon:'none',
                title: '请选择开始日期',
            })
            return
        }
        if(this.data.dateEnd == ''){
            wx.showToast({
                icon:'none',
                title: '请选择截止日期',
            })
            return
        }
        this.getData(this.data.status)
    },
    //我已配送
    handleSend(e){
        let that = this;
        //console.log(e)
        let $id = e.currentTarget.dataset.id
        wx.showLoading({
            title: 'loading...',
        })
        that.setData({
            btnStatus:true
        })
        util.request(api.supplier.updateStatus+'?id='+$id,{},'post').then(function(res) {
            wx.showModal({
                title: '提示',
                content:'您的订单已配送',
                confirmColor:'#F96A42',
                showCancel:false,
                success (res) {
                  if (res.confirm) {
                      that.getData(that.data.status)
                  } 
                }
            })
            that.setData({
                btnStatus:false
            })
            wx.hideLoading()
        }).catch(err=>{
            console.log(err)
            wx.showToast({
                title: err.message,
                icon:'error'
            })
            that.setData({
                btnStatus:false
            })
            wx.hideLoading()
        })
    },
    getData(status){
        let that = this;
        that.setData({
            pageNo: 1,
            hasMore:true,
            orderList:[]

        });
        wx.showLoading({
            title: '加载中...',
        })
        let starTime = this.data.dateStart!=''?this.data.dateStart+' 00:00:00':''
        let endTime = this.data.dateEnd!=''?this.data.dateEnd+' 00:00:00':''
        var url = api.supplier.orderList + "?pageNo=" +that.data.pageNo + '&pageSize=' + that.data.pageSize+ '&status=' + status+'&supplierId='+that.data.supplierId+'&endTime='+endTime+'&startTime='+starTime;
        util.request(url,'get').then(function(res) {
            that.setData({
                orderList: res.result
            });
            if (res.result.length < that.data.pageSize) {
                that.setData({
                    hasMore: false,
                })
            }
            wx.stopPullDownRefresh();
            wx.hideLoading()
        }).catch(err=>{
            console.log(err)
            wx.hideLoading()
            wx.stopPullDownRefresh() 
        })
    },
    loadMore: function(status) {
        var that = this;
        if (!this.data.hasMore) return
        wx.showLoading({
            title: '加载中...',
        })
        let starTime = this.data.dateStart!=''?this.data.dateStart+' 00:00:00':''
        let endTime = this.data.dateEnd!=''?this.data.dateEnd+' 00:00:00':''
        var url = api.supplier.orderList + "?pageNo=" + (++that.data.pageNo) + '&pageSize=' + that.data.pageSize+ '&status=' + status+'&supplierId='+that.data.supplierId+'&endTime='+endTime+'&startTime='+starTime;
        util.request(url,'get').then(function(res) {
            if (res.code == 200) {
                that.setData({
                    orderList: that.data.orderList.concat(res.result)
                });
                if (res.result.length < that.data.pageSize) {
                    that.setData({
                        hasMore: false,
                    })
                }
                wx.hideLoading()
            }
        });
    },
    onChange(e) {
        const index = e.detail.index
        this.setData({
            activeTab: index
        })
    },
    switchTab(e){
        const id = e.currentTarget.dataset.id;
        this.setData({
            activeTab: id,
            dateStart:'',
            dateEnd:''
        })
        if(this.data.activeTab === 0){
            this.setData({
                status:''
            })
        } else if(this.data.activeTab === 1){
            this.setData({
                status:1
            })
        } else if(this.data.activeTab === 2){
            this.setData({
                status:2
            })
        }else if(this.data.activeTab === 3){
            this.setData({
                status:4
            })
        }
        this.getData(this.data.status)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.loadMore(this.data.status)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})